package com.yootk.main;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.Arrays;
import java.util.List;

/**
 * List集合表达式
 */
public class Demo05_List {
    private static final Logger LOGGER = LoggerFactory.getLogger(Demo03_Expression.class);

    public static void main(String[] args) {
//        test1();
//        test2();
//        test3();
        test4();
    }

    /**
     * 创建List集合
     */
    public static void test1() {
        String content = "#{ {'www.yootk.com', 'jixianit.com', 'edu.yootk.com'} }";
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression(content, ParserContext.TEMPLATE_EXPRESSION);
        EvaluationContext context = new StandardEvaluationContext();
        List<String> result = exp.getValue(context, List.class);    // 直接转为List集合
        LOGGER.info("结果：{}", result);
    }

    /**
     * 访问外部集合
     */
    public static void test2() {
        List<String> data = List.of("www.yootk.com", "www.jixianit.com", "edu.yootk.com");
        String content = "#{ #all[0] }";
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression(content, ParserContext.TEMPLATE_EXPRESSION);
        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("all", data);   // 设置变量内容
        LOGGER.info("结果：{}", exp.getValue(context, String.class));
    }

    /**
     * 修改指定索引的数据
     */
    public static void test3() {
        List<String> data = Arrays.asList("www.yootk.com", "www.jixianit.com", "edu.yootk.com");
        String content = "#{ #all[2] = 'book.yootk.com' }";
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression(content, ParserContext.TEMPLATE_EXPRESSION);
        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("all", data);   // 设置变量内容
        LOGGER.info("表达式计算结果：{}", exp.getValue(context, String.class));
        LOGGER.info("修改后List集合：{}", data);
    }

    /**
     * 迭代修改List集合数据
     */
    public static void test4() {
        List<String> data = Arrays.asList("www.yootk.com", "www.jixianit.com", "edu.yootk.com");
        String content = "#{ #all.!['【学习资源】' + #this] }";
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression(content, ParserContext.TEMPLATE_EXPRESSION);
        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("all", data);   // 设置变量内容
        LOGGER.info("表达式计算结果：{}", exp.getValue(context, List.class));
        LOGGER.info("修改后List集合：{}", data);      // 原始List集合不会发生改变
    }
}
